<?php
class UserController extends AdminController {
	public function UserController(){
		parent::AdminController();
	}
	
	public function index(){
		$this->init_page_info();
		$user_mod = new UserModel();
		$user_list = $user_mod->user_list();
		$this->set_page_total($user_mod->count());
		$this->assign('user_list', $user_list);
		$this->display();
	}
	
	public function edit(){
		$this->display();
	}
	
	public function role(){
		$role_mod = new RoleModel();
		$roles = $role_mod->find(array('site_id'=>SITE_ID));
		$this->assign('roles', $roles);
		
		
		$this->display();
	}
	
	public function role_edit(){
		$acl_mod = new AclModel();
		$role_mod = new RoleModel();
		
		$acls = $acl_mod->find();

		if($this->id != null){
			$role = $role_mod->get_by_key($this->id);
			$this->assign('role', $role);
			
			foreach($acls as $namespace=>$acl_infos){
				foreach($acl_infos['items'] as $key=>$acl){
					$acls[$namespace]['items'][$key]['attr'] = ACLHelper::check($namespace, $acl['acl'], $role['acl'][$namespace])?'checked=\'checked\'':'';
				}
			}
		}
		$this->assign('acls', $acls);
		$this->assign('site_id', $this->id);
		
		if(IS_POST){
			$role = array(
				'name' 			=> trim($_POST['name']),
				'site_id' 		=> trim($_POST['site_id']),
				'desc' 	=> trim($_POST['desc']),
			);
//			$namespaces = $acl_mod->get_namespaces();
			$role_acls = array();
			foreach($acls as $namespace=>$acl_infos){
				if(isset($_POST[$namespace])){
					$role_acls[$namespace] = $_POST[$namespace];
				}
			}
			$acl_array = ACLHelper::parse_acl_array($role_acls);
			$role['acl'] = $acl_array;
			
			if($this->id != null){
				$role_mod->update_by_key($this->id, $role);
				$this->assign('info', '修改成功！');
			}else{
				$role_mod->insert($role);
				$this->assign('info', '添加成功！');
			}
		}
		
		$this->display();
	}
	
	public function set_pwd(){
		$user_mod = new UserModel();
		$user = $user_mod->get_by_key($this->id, 'user_name,password,status');
		if($user == null){
			
		}
		$this->assign('user', $user);
		if(IS_POST){
			if($this->set_password($user)){
				$this->assign('info', '密码设置成功！');
			}
		}
		$this->display();
	}
	
	private function set_password($user){
		$pwd = trim($_POST['pwd']);
		$pwd1 = trim($_POST['pwd1']);
		if(strlen($pwd)<6){
			//密码长度不能小于6
			$this->assign('error', '密码长度不能小于6个字符！');
			return false;
		}
		if($pwd!=$pwd1){
			$this->assign('error', '两次输入密码不一致，请重新输入！');
			return false;
		}
		
		$result = UCenter::set_password($this->id, $pwd);
		return true;
	}
}

?>